<html><head>
      <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
   <title>2.&nbsp;Configuring Twitter Connectivity</title><link rel="stylesheet" href="css/manual.css" type="text/css"><meta name="generator" content="DocBook XSL Stylesheets V1.75.2"><link rel="home" href="index.html" title="Spring Social Twitter Reference Manual"><link rel="up" href="index.html" title="Spring Social Twitter Reference Manual"><link rel="prev" href="overview.html" title="1.&nbsp;Spring Social Twitter Overview"><link rel="next" href="apis.html" title="3.&nbsp;Twitter API Binding"><!--Begin Google Analytics code--><script type="text/javascript">
			var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
			document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
		</script><script type="text/javascript">
			var pageTracker = _gat._getTracker("UA-2728886-3");
			pageTracker._setDomainName("none");
			pageTracker._setAllowLinker(true);
			pageTracker._trackPageview();
		</script><!--End Google Analytics code--></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">2.&nbsp;Configuring Twitter Connectivity</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="overview.html">Prev</a>&nbsp;</td><th width="60%" align="center">&nbsp;</th><td width="20%" align="right">&nbsp;<a accesskey="n" href="apis.html">Next</a></td></tr></table><hr></div><div class="chapter" title="2.&nbsp;Configuring Twitter Connectivity"><div class="titlepage"><div><div><h2 class="title"><a name="connecting"></a>2.&nbsp;Configuring Twitter Connectivity</h2></div></div></div>
			

	<p>
		Spring Social's <code class="classname">ConnectController</code> works with one or more provider-specific <code class="classname">ConnectionFactory</code>s to exchange authorization details with the provider and to create connections. 
		Spring Social Twitter provides <code class="classname">TwitterConnectionFactory</code>, a <code class="classname">ConnectionFactory</code> for creating connections with Twitter.
	</p>

	<p>
		So that <code class="classname">ConnectController</code> can find <code class="classname">TwitterConnectionFactory</code>, it must be registered with a <code class="classname">ConnectionFactoryRegistry</code>.
		The following class constructs a <code class="classname">ConnectionFactoryRegistry</code> containing a <code class="interfacename">ConnectionFactory</code> for Twitter using Spring's Java configuration style: 		
	</p>
	<pre class="programlisting">
<i><span class="hl-annotation" style="color: gray">@Configuration</span></i>
<span class="hl-keyword">public</span> <span class="hl-keyword">class</span> SocialConfig {

    <i><span class="hl-annotation" style="color: gray">@Inject</span></i>
    <span class="hl-keyword">private</span> Environment environment;
	
    <i><span class="hl-annotation" style="color: gray">@Bean</span></i>
    <span class="hl-keyword">public</span> ConnectionFactoryLocator connectionFactoryLocator() {
        ConnectionFactoryRegistry registry = <span class="hl-keyword">new</span> ConnectionFactoryRegistry();
        registry.addConnectionFactory(<span class="hl-keyword">new</span> TwitterConnectionFactory(
            environment.getProperty(<span class="hl-string">"twitter.consumerKey"</span>),
            environment.getProperty(<span class="hl-string">"twitter.consumerSecret"</span>)));
        <span class="hl-keyword">return</span> registry;
    }

}

	</pre>

	<p>
		Here, a Twitter connection factory is registered with <code class="classname">ConnectionFactoryRegistry</code> via the <code class="methodname">addConnectionFactory()</code> method.
		If we wanted to add support for connecting to other providers, we would simply register their connection factories here in the same way as <code class="classname">TwitterConnectionFactory</code>.
	</p>
	
	<p>
		Because consumer keys and secrets may be different across environments (e.g., test, production, etc) it is recommended that these values be externalized. 
		As shown here, Spring 3.1's <code class="interfacename">Environment</code> is used to look up the application's consumer key and secret.
	</p>	

	<p>
		Optionally, you may also configure <code class="classname">ConnectionFactoryRegistry</code> and <code class="classname">TwitterConnectionFactory</code> in XML:
	</p>
		
	<pre class="programlisting">
<span class="hl-tag">&lt;bean</span> <span class="hl-attribute">id</span>=<span class="hl-value">"connectionFactoryLocator"</span> <span class="hl-attribute">class</span>=<span class="hl-value">"org.springframework.social.connect.support.ConnectionFactoryRegistry"</span><span class="hl-tag">&gt;</span>
    <span class="hl-tag">&lt;property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"connectionFactories"</span><span class="hl-tag">&gt;</span>
        <span class="hl-tag">&lt;list&gt;</span>
            <span class="hl-tag">&lt;bean</span> <span class="hl-attribute">class</span>=<span class="hl-value">"org.springframework.social.twitter.connect.TwitterConnectionFactory"</span><span class="hl-tag">&gt;</span>
                <span class="hl-tag">&lt;constructor-arg</span> <span class="hl-attribute">value</span>=<span class="hl-value">"${twitter.consumerKey}"</span><span class="hl-tag"> /&gt;</span>
                <span class="hl-tag">&lt;constructor-arg</span> <span class="hl-attribute">value</span>=<span class="hl-value">"${twitter.consumerSecret}"</span><span class="hl-tag"> /&gt;</span>				
            <span class="hl-tag">&lt;/bean&gt;</span>
        <span class="hl-tag">&lt;/list&gt;</span>
    <span class="hl-tag">&lt;/property&gt;</span>
<span class="hl-tag">&lt;/bean&gt;</span>
	</pre>	

	<p>
		This is functionally equivalent to the Java-based configuration of <code class="classname">ConnectionFactoryRegistry</code> shown before.
		The only casual difference is that the connection factories are injected as a list into the <code class="code">connectionFactories</code> property rather than with the <code class="methodname">addConnectionFactory()</code> method.
		As in the Java-based configuration, the application's consumer key and secret are externalized (shown here as property placeholders).
	</p>	
		
	<p>
		Refer to <a class="ulink" href="http://static.springsource.org/spring-social/docs/1.0.x/reference/html/connecting.html" target="_top">Spring Social's reference documentation</a> for complete details on configuring <code class="classname">ConnectController</code> and its dependencies.
	</p>
			
</div><!--Begin LoopFuse code--><script src="http://loopfuse.net/webrecorder/js/listen.js" type="text/javascript"></script><script type="text/javascript">
			_lf_cid = "LF_48be82fa";
			_lf_remora();
		</script><!--End LoopFuse code--><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="overview.html">Prev</a>&nbsp;</td><td width="20%" align="center">&nbsp;</td><td width="40%" align="right">&nbsp;<a accesskey="n" href="apis.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">1.&nbsp;Spring Social Twitter Overview&nbsp;</td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top">&nbsp;3.&nbsp;Twitter API Binding</td></tr></table></div></body></html>